package com.easyadmin.modules.stock.controller;

import com.easyadmin.common.aop.annotation.Repeat;
import com.easyadmin.common.web.base.BaseController;
import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.common.web.domain.response.Result;
import com.easyadmin.common.web.domain.response.module.ResultTable;
import com.easyadmin.modules.stock.domain.SysStockOptions;
import com.easyadmin.modules.stock.service.ISysStockOptionsService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 期权产品Controller
 *
 * @author jmys
 * @date 2025-10-02
 */
@RestController
@RequestMapping("/stock/stockOptions")
public class SysStockOptionsController extends BaseController {

    private String prefix = "stock/stockOptions";

    @Autowired
    private ISysStockOptionsService sysStockOptionsService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/stock/stockOptions/main','stock:stockOptions:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询期权产品列表
     */
    @GetMapping("/data")
    @ApiOperation(value = "查询期权产品列表")
    @PreAuthorize("hasPermission('/stock/stockOptions/data','stock:stockOptions:data')")
    public ResultTable list(@ModelAttribute SysStockOptions sysStockOptions, PageDomain pageDomain) {
        PageInfo<SysStockOptions> pageInfo = sysStockOptionsService.selectSysStockOptionsPage(sysStockOptions, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增期权产品
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/stock/stockOptions/add','stock:stockOptions:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增期权产品
     */
    @Repeat
    @PostMapping("/save")
    @ApiOperation(value = "新增期权产品")
    @PreAuthorize("hasPermission('/stock/stockOptions/add','stock:stockOptions:add')")
    public Result save(@RequestBody SysStockOptions sysStockOptions) {
        return decide(sysStockOptionsService.save(sysStockOptions));
    }

    /**
     * 修改期权产品
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/stock/stockOptions/edit','stock:stockOptions:edit')")
    public ModelAndView edit(Long id, ModelMap map) {
        SysStockOptions sysStockOptions =sysStockOptionsService.getById(id);
        map.put("sysStockOptions", sysStockOptions);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改期权产品
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改期权产品")
    @PreAuthorize("hasPermission('/stock/stockOptions/edit','stock:stockOptions:edit')")
    public Result update(@RequestBody SysStockOptions sysStockOptions) {
        return decide(sysStockOptionsService.update(sysStockOptions));
    }

    /**
     * 删除期权产品
     */
    @DeleteMapping("/batchRemove")
    @ApiOperation(value = "删除期权产品")
    @PreAuthorize("hasPermission('/stock/stockOptions/remove','stock:stockOptions:remove')")
    public Result batchRemove(String ids) {
        return decide(sysStockOptionsService.batchRemove(ids.split(",")));
    }

    /**
     * 删除期权产品
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除期权产品")
    @PreAuthorize("hasPermission('/stock/stockOptions/remove','stock:stockOptions:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(sysStockOptionsService.remove(id));
    }
}
